/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.xtd.view.finance;

import com.xtd.facade.settlement.FormFacade;
import com.xtd.facade.settlement.PostingFacade;
import com.xtd.model.settlement.Form;
import com.xtd.model.settlement.Posting;
import com.xtd.util.FormTypeEnum;
import com.xtd.util.PostingTypeEnum;
import com.xtd.view.AbstractManager;
import com.xtd.view.SessionBean;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Lu Yuanbin
 */
@ManagedBean
@SessionScoped
public class FinanceManager extends AbstractManager {

    @EJB
    FormFacade formFacade;
    @EJB
    PostingFacade postingFacade;
    private List<Form> formList;
    private Posting posting;
    private FormTypeEnum formType;
    private String formNumber;
    private Form form;
    @ManagedProperty(value = "#{sessionBean}")
    private SessionBean sessionBean;
    private List<Posting> postings;
    private Date selectedDate;

    /** Creates a new instance of FinanceManager */
    public FinanceManager() {
    }

    public void findUnSettledForm(ActionEvent actionEvent) {
        this.formList.clear();
        this.formList.addAll(formFacade.findUnSettledForm());
    }

    public void search(ActionEvent actionEvent) {
        this.formList.clear();
        this.formList.addAll(formFacade.findByNumber(formNumber, formType));
    }

    public void searchByType() {
        this.formList.clear();
        this.formList.addAll(formFacade.findByType(formType));
    }

    public void searchToadyPostings(ActionEvent actionEvent) {
        this.postings.clear();
        this.postings.addAll(postingFacade.findTodayPostings());
    }

    public void searchPostings(ActionEvent actionEvent) {
        this.postings.clear();
        this.postings.addAll(postingFacade.findByNumber(formNumber, formType, getSelectedDate()));
    }

    public void searchPostingsByType() {
        this.postings.clear();
        this.postings.addAll(postingFacade.findByType(formType, getSelectedDate()));
    }

    public void confirmSettlement(ActionEvent actionEvent) {
        RequestContext context = RequestContext.getCurrentInstance();

        if (posting.getForm().getType().isIncome()) {
            if (!posting.getType().isIncome()) {
                if (posting.getAmount().add(posting.getForm().getTotalExpense()).compareTo(posting.getForm().getTotalPayment()) > 0) {
                    addMessage("确认失败", null, FacesMessage.SEVERITY_INFO);
                    context.addCallbackParam("success", false);
                    return;
                }
            }
        }
        try {
            postingFacade.update(posting);
            posting.getForm().getPostingCollection().add(posting);
            addMessage("保存成功", null, FacesMessage.SEVERITY_INFO);
            context.addCallbackParam("success", true);
        } catch (Exception e) {
            getLogger(this.getClass()).log(Level.FINEST, "结算失败", e);
            addMessage("确认失败", "请稍候再试", FacesMessage.SEVERITY_ERROR);
            context.addCallbackParam("success", false);
        }
    }

    public String receive(Form form, String postingType) {
        RequestContext context = RequestContext.getCurrentInstance();
        if (form.isSettled()) {
            addMessage("无法收款", "该结算单已完成", FacesMessage.SEVERITY_ERROR);
            context.addCallbackParam("success", false);
            return null;
        }
        context.addCallbackParam("success", true);
        posting = new Posting(form, PostingTypeEnum.valueOf(postingType), getSessionBean().getUser());
        return null;
    }

    public String pay(Form form, String postingType) {
        RequestContext context = RequestContext.getCurrentInstance();
        if (form.isSettled()) {
            addMessage("无法收款", "该结算单已完成", FacesMessage.SEVERITY_ERROR);
            context.addCallbackParam("success", false);
            return null;
        }

        context.addCallbackParam("success", true);
        posting = new Posting(form, PostingTypeEnum.valueOf(postingType), getSessionBean().getUser());
        return null;
    }

    public String select(Form form) {
        this.form = form;
        return null;
    }

    /**
     * @return the formList
     */
    public List<Form> getFormList() {
        if (formList == null) {
            formList = new ArrayList<Form>();
        }
        return formList;
    }

    /**
     * @param formList the formList to set
     */
    public void setFormList(List<Form> formList) {
        this.formList = formList;
    }

    /**
     * @return the posting
     */
    public Posting getPosting() {
        return posting;
    }

    /**
     * @param posting the posting to set
     */
    public void setPosting(Posting posting) {
        this.posting = posting;
    }

    /**
     * @return the formType
     */
    public FormTypeEnum getFormType() {
        return formType;
    }

    /**
     * @param formType the formType to set
     */
    public void setFormType(FormTypeEnum formType) {
        this.formType = formType;
    }

    /**
     * @return the formNumber
     */
    public String getFormNumber() {
        return formNumber;
    }

    /**
     * @param formNumber the formNumber to set
     */
    public void setFormNumber(String formNumber) {
        this.formNumber = formNumber;
    }

    /**
     * @return the form
     */
    public Form getForm() {
        return form;
    }

    /**
     * @param form the form to set
     */
    public void setForm(Form form) {
        this.form = form;
    }

    /**
     * @return the sessionBean
     */
    public SessionBean getSessionBean() {
        return sessionBean;
    }

    /**
     * @param sessionBean the sessionBean to set
     */
    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    /**
     * @return the postings
     */
    public List<Posting> getPostings() {
        if(postings == null) {
            postings = new ArrayList();
        }
        return postings;
    }

    /**
     * @param postings the postings to set
     */
    public void setPostings(List<Posting> postings) {
        this.postings = postings;
    }

    /**
     * @return the selectedDate
     */
    public Date getSelectedDate() {
        return selectedDate;
    }

    /**
     * @param selectedDate the selectedDate to set
     */
    public void setSelectedDate(Date selectedDate) {
        this.selectedDate = selectedDate;
    }


}
